/**
 * 
 */
package org.df.ulab.util.formatter;

import java.text.DecimalFormat;


/**
 * @since 04/05/2009 
 */
public class Formatter {

	private DecimalFormat dfTimeUnit;
	
	public Formatter() {
		dfTimeUnit = new DecimalFormat("0");
	}

	public String formatTimeInMilliToHour(long millis) {
		if (millis < 1000)
			return "0";
		
		return formatTimeInSecondsToHour((int)(millis / 1000));
	}
	
	public String formatTimeInSecondsToHour(long seconds) {
		if (seconds < 1)
			return "0";
		
		int hour = 0;
		int minute = 0;
		
		if (seconds >= 60) {
			minute = (int)(seconds / 60);
			seconds -= (minute * 60); 
		}
		
		if (minute >= 60) {
			hour = (minute / 60);
			minute -= (hour * 60);
		}
		
		return formatTime(hour, minute, (int)seconds);
	}
	
	public String formatTime(int hour, int minute, int seconds) {
		StringBuilder builder = new StringBuilder();
		
		if (hour > 0) {
			builder.append(dfTimeUnit.format(hour));
			builder.append("h ");
		}
		if (minute > 0 || (hour > 0 && seconds > 0)) {
			builder.append(dfTimeUnit.format(minute));
			builder.append("min ");
		}
		if (seconds > 0) {
			builder.append(dfTimeUnit.format(seconds));
			builder.append("sec ");
		}
		
		return builder.toString();
	}
}
